package com.example.springcloudribbonclient.controller;

import com.example.springcloudribbonclient.server.ClientServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class ClientController {
    @Autowired
    private ClientServer clientServer;

    @RequestMapping("/client")
    public String client() {
        return clientServer.client() + ":8088进入";
    }

    @RequestMapping("/abc")
    public String clientABC() {
        return clientServer.client() + ":abc2进入";
    }

    @RequestMapping("/postman")
    public String getPost(@RequestParam("id") String id, @RequestParam("flag") String flag) {
        System.out.println("打印flag:" + flag);
        System.out.println("输出分隔符：----------------------------");
        return id;
    }


//    @RequestMapping("/demoHystrixCollasper")
//    public String demoHystrixCollasper(String name) {
//        Future<String> result1 = clientServer.clientHystrix("张三");
//        Future<String> result2 = clientServer.clientHystrix("李四");
//        try {
//            System.out.println(result1.get());
//            System.out.println(result2.get());
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//
//        return "OK";
//    }

    @RequestMapping("/demoHystrixCollasper")
    public String demoHystrixCollasper(String name) {
        Future<String> result1 = clientServer.clientHystrix("张三");
        Future<String> result2 = clientServer.clientHystrix("李四");
        try {
            System.out.println(result1.get());
            System.out.println(result2.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return "OK";
    }


    @RequestMapping("thread")
    public String thread() {
        clientServer.thread1();
        clientServer.thread2();

        return null;
    }

    @RequestMapping("semphare")
    public String semphare() {
        return clientServer.semaphore();
    }


}
